package cn.otrue.patienthealthmanager.adapter;

import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.DensityUtil;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class OfMainInfoAdapter extends BaseAdapter {

	private List<Map<String, String>> buyy;
	private LayoutInflater inflater;
	private Context context;
	private TextView tv_main_info_name;
	private TextView tv_main_info_time;
	private TextView tv_main_info_other;
	private ImageView iv_main_info_img;

	 public void ImageSet(String imgurl, ImageView img_headPhoto) {
	
	 DisplayImageOptions options = new DisplayImageOptions.Builder()
	 .showImageOnLoading(R.drawable.iv_user_photo)
	 .showImageOnFail(R.drawable.iv_user_photo)
	 .showImageForEmptyUri(R.drawable.iv_user_photo)
	 .cacheInMemory(true)
	 .cacheOnDisk(true)
	 .bitmapConfig(Bitmap.Config.RGB_565)
	 .displayer(
	 new RoundedBitmapDisplayer(DensityUtil.dip2px(context,
	 45))).build();
	 ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	 }
	public OfMainInfoAdapter(Context context, List<Map<String, String>> buyy) {
		super();
		this.buyy = buyy;
		inflater = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public int getCount() {

		return buyy.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		View view = inflater.inflate(R.layout.ofitem_main_info_lv, null);
		tv_main_info_other = (TextView) view
				.findViewById(R.id.tv_main_info_other);
		tv_main_info_name = (TextView) view
				.findViewById(R.id.tv_main_info_name);
		tv_main_info_time = (TextView) view
				.findViewById(R.id.tv_main_info_time);
		iv_main_info_img = (ImageView) view.findViewById(R.id.iv_main_info_im);

		Map<String, String> map = buyy.get(arg0);

		tv_main_info_name.setText("标题:" + map.get("title"));

		try {
			long num = Long.parseLong(map.get("createtime"));
			Date data = new Date(num);

			String dateStr = CommonUtil.getFormatDate("yyyy-MM-dd", data);
			tv_main_info_time.setText("时间:" + dateStr);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tv_main_info_other.setText("内容:" + map.get("content"));

		try {
			String url= map.get("headimg");
			if (url!=null) {
				ImageSet(url,iv_main_info_img);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return view;
	}
}
